/*
 * Image.cpp
 *
 * Copyright (c) 2009-2011 [BRN] aka. SWW13.
 *
 * This file is part of [BRN]Lib.
 *
 * [BRN]Lib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * [BRN]Lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with [BRN]Lib.  If not, see <http ://www.gnu.org/licenses/>.
 */

#if !defined BRNlib_NoD3D
#include "Image.h"

namespace BRNlib
{
	namespace D3D
	{
		namespace Core
		{
			Image::Image(D3DXDevice *pDevice, char *FileName)
			{
				this->FileName = FileName;
				this->pDevice = pDevice;

				Create();
			}

			Image::~Image(void)
			{
				Delete();
				delete[] FileName;
			}

			void Image::Draw(int PosX, int PosY, int Alpha, float ScaleX, float ScaleY)
			{
					D3DXMATRIX scaleMatrix; 
					D3DXMATRIX transMatrix;
					D3DXMatrixScaling(&scaleMatrix, ScaleX, ScaleY, 0.0f);
					D3DXMatrixTranslation(&transMatrix, (float)PosX, (float)PosY, 0.0f);

					#if defined BRNlib_D3D9
					D3DXMatrixMultiply(&transMatrix, &scaleMatrix, &transMatrix);
					pSprite->SetTransform(&transMatrix);

					pSprite->Begin(D3DXSPRITE_ALPHABLEND);
					pSprite->Draw(pTexture, NULL, NULL, NULL, D3DCOLOR_RGBA(255, 255, 255, 255-int(2.55*Alpha)));
					pSprite->End();
					#endif

					#if defined BRNlib_D3D8
					D3DXMatrixMultiply(&transMatrix, &scaleMatrix, &transMatrix);

					pSprite->Begin();
					pSprite->DrawTransform(pTexture, NULL, &transMatrix, D3DCOLOR_RGBA(255, 255, 255, 255-int(2.55*Alpha)));
					pSprite->End();
					#endif
			}

			POINT Image::GetSize()
			{
				return Size;
			}

			void Image::OnPreReset()
			{
				Delete();
			}
			void Image::OnPostReset()
			{
				Create();
			}
			void Image::OnRelease()
			{
				Delete();
			}

			void Image::Create()
			{
				D3DXCreateSprite(pDevice, &pSprite);

				D3DXCreateTextureFromFileExA(pDevice, FileName, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0,
											D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_FILTER_NONE,
											D3DX_FILTER_NONE, 0xFFFF00FF, NULL, NULL, &pTexture);

				D3DXIMAGE_INFO ImageInfo;
				D3DXGetImageInfoFromFileA(FileName, &ImageInfo);

				this->Size.x = ImageInfo.Width;
				this->Size.y = ImageInfo.Height;
			}
			void Image::Delete()
			{
				pSprite->Release();
				pSprite = NULL;

				pTexture->Release();
				pTexture = NULL;
			}
		}
	}
}

#endif